package com.aaa.web.service.impl.carmessageserviceimpl;

import com.aaa.entity.Employee;
import com.aaa.entity.storecar.StoreCarMessage;
import com.aaa.util.EmpUtil;
import com.aaa.web.commons.Result;
import com.aaa.web.commons.StoreCarQuery;
import com.aaa.web.dao.carmessagedao.StoreCarMessageDao;
import com.aaa.web.service.carmessageservice.StoreCarMessageService;
import com.aaa.web.systemconst.ISystemConst;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @program: ${PROJECT_NAME}
* @description: 门店电瓶车管理页面
* @author: 许一鑫
* @create: 9:51 2019/1/3
**/
@Service
public class StoreCarMessageServiceImpl implements StoreCarMessageService {
    /**自动注入dao*/
    @Autowired
    private StoreCarMessageDao dao;
    /**查询所有门店电瓶车信息包含门店名，车型号， 出厂码，生产日期，售出日期，是否售出*/
    @Override
    public Map getStoreMessage(StoreCarQuery query) {
        int page=query.getPage();
        int size=query.getLimit();
        PageHelper.startPage(page,size);
        //获取登录员工
        Employee emp=EmpUtil.getEmp();
        query.setEmpschoolid(emp.getSchoolid());
        //存放返回信息的map
        Map map=new HashMap();
        //查询所有信息
        List<StoreCarMessage> list=dao.getStoreMessage(query);
        //返回条数
        Integer count=dao.getStoreCount(query);
        map.put("data",list);
        map.put("count",count);
        map.put("code",ISystemConst.SU_COD);
        map.put("msg","");
        return map;
    }

    /**添加新的门店车辆信息，车型号，出厂码，门店，出厂日期*/
    @Override
    public Result addNewStoreCar(StoreCarMessage s) {

        //获取员工schoolid
        Employee emp=EmpUtil.getEmp();
        //设置返回值
        Result result=new Result();

        //判断出厂码唯一性
        String carcode=dao.findcarcode(s.getCarcode());

        if (carcode!=null){
            //如果已经存在则返回300
            result.setCode(ISystemConst.DISABLE_CODE);
            return result;
        }
        //判断员工的schoolid与添加的schoolid是否相同
        if (s.getSchoolid()==emp.getSchoolid()){
            //是则继续
            dao.addNewStoreCar(s);
            result.setCode(ISystemConst.SUCCESS_CODE);
            return result;
        }
        //否则返回失败码
       result.setCode(ISystemConst.DEFEAT_CODE);
        return result;
    }

    /**删除新增错误的入库信息*/
    @Override
    public Result delStoreCar(StoreCarMessage s) {
        //设置返回值
        Result result=new Result();
        //获取员工schoolid
        Employee emp=EmpUtil.getEmp();
        Integer empschoolid = emp.getSchoolid();
        //查询该车辆所在的门店
        Integer schoolid = dao.findschoolid(s);
        if (empschoolid.equals(schoolid)){
            dao.delStoreCar(s);
            //提交车主信息成功后返回100
            result.setCode(ISystemConst.SUCCESS_CODE);
            return result;
        }
        //失败返回200
        result.setCode(ISystemConst.DEFEAT_CODE);
        return result;
    }

    /**提交车主信息*/
    @Override
    public Result addCarUser(StoreCarMessage s) {
        //设置返回值
        Result result=new Result();
        //获取员工schoolid
        Employee emp=EmpUtil.getEmp();
        Integer empschoolid = emp.getSchoolid();
        //查询该车辆所在的门店
        Integer schoolid = dao.findschoolid(s);
        if (empschoolid.equals(schoolid)){
            dao.addCarUser(s);
            //提交车主信息成功后返回100
            result.setCode(ISystemConst.SUCCESS_CODE);
            return result;
        }
       //失败返回200
       result.setCode(ISystemConst.DEFEAT_CODE);
        return result;
    }
}
